跳到主要内容

命令行操作

进入命令行

./hbase shell

命名空间

列出命名空间

list_namespace

创建命名空间

create_namespace 'bigdata'

指定命名空间建表并创建列族

创建表并且指定一个info的column family

create 'bigdata:student','info'
create 'tt:tt',{NAME => 'f1', BLOOMFILTER => 'ROW', IN_MEMORY => 'false', VERSIONS => '1', KEEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', COMPRESSION => 'LZ4', TTL => 'FOREVER', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '65536', REPLICATION_SCOPE => '0'},{NUMREGIONS => 5, SPLITALGO => 'HexStringSplit'}

查看指定表信息

describe 'test:test_good'

删除命名空间

只能删除空的命名空间,如果不为空,需要先删除该命名空间下的所有表

drop_namespace 'bigdata'

表操作

建表

create 'student','info'

指定压缩格式

create 'mytable', {NAME=>'cf1', COMPRESSION=>'SNAPPY'}

创建多个列族

create 'test:test_goods', {NAME=>'f1',COMPRESSION => 'LZ4'}, {NAME=>'f3',COMPRESSION => 'LZ4'}

查看表结构

describe 'student'

删除表

首先需要先让该表为 disable 状态

disable 'student'

然后才能 drop 这个表

drop 'student'

修改表

alter 'student',{NAME=>'info',VERSIONS=>3}

增加列族

alter 'test:test_goods','f1'

删除info列族

disable 'test:test_goods'
alter 'test:test_goods', {NAME=>'info', METHOD=>'delete'}
describe 'test:test_goods'
enable 'test:test_goods'

查看所有表

list

数据操作

插入数据

put 'student','1001','info:sex','male'
put 'student','1001','info:age','18'
put 'student','1002','info:name','Janna'
put 'student','1002','info:sex','female'
put 'student','1002','info:age','20'

删除数据

删除某 rowkey 的全部数据

deleteall 'student','1001'

删除某 rowkey 的某一列数据

delete 'student','1002','info:sex'

提示:清空表的操作顺序为先 disable,然后再 truncate。

truncate 'student'

查看数据

scan 'student'

范围查找

scan 'student',{STARTROW => '1001', STOPROW  => '1001'}
scan 'student',{STARTROW => '1001'}

查看 “指定行” 或 “指定列族:列” 的数据

get 'student','1001'
get 'student','1001','info:name'

时间查找

scan 'erec:table', { COLUMN => 'f1:h', TIMERANGE => [1711500934443, 1711500994443]}

统计表行数

count 'student'

初始化hbase

# 停止hbase服务
> stop-hbase.sh

# 进入zookeeper命令行
> zkCli.sh

# 删除/hbase节点
[zk: localhost:2181(CONNECTED) 0] rmr /hbase

# 删除hdfs的/hbase目录
> hdfs dfs -rm -R /hbase

# 启动hbase服务
> start-hbase.sh

# 验证问题解决,建表成功
> hbase shell
hbase:002:0> create 'table1', 'cf1'
2022-03-15 12:59:12,870 INFO [main] client.HBaseAdmin (HBaseAdmin.java:postOperationResult(3591)) - Operation: CREATE, Table Name: default:table1, procId: 9 completed
Created table table1
Took 1.2251 seconds
=> Hbase::Table - table1